/*
加入过的俱乐部界面，记录了玩家加入过的俱乐部，这里的数据是本地存储的不存在服务器
*/
var confige = require("confige");
var tipsConf = require("tips").tipsConf;

cc.Class({
    extends: cc.Component,

    properties: {
        roomItem:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.roomListView = this.node.getChildByName("roomList");
        this.roomListContent = this.roomListView.getChildByName("view").getChildByName("content");
        this.roomItemBeginY = 40;
        this.roomItemOffsetY = 80;
        this.roomItemList = {};

        this.isInit = true;
    },

    updateLayer:function(){
        for(var i in this.roomItemList)
        {
            this.roomItemList[i].destroy();
        }
        this.roomItemList = {};

        var roomData = [];
        for(var i in window.localSetting.roomHistory)
            roomData.push(window.localSetting.roomHistory[i]);
        roomData = window.DKTools.quickSort(roomData,"time");
        var roomItemCount = 0;
        var newRoomItemY = this.roomItemBeginY;
        for(var i in roomData)
        {
            roomItemCount++;
            var newRoomItem = cc.instantiate(this.roomItem);
            newRoomItem.y = -newRoomItemY;
            this.roomItemList[i] = newRoomItem;
            this.roomListContent.addChild(newRoomItem);
            newRoomItem.active = true;
            newRoomItemY += this.roomItemOffsetY;

            var self = this;
            var btnJoin = newRoomItem.getChildByName("join").getComponent("cc.Button");
            var joinCallBack = function(){
                console.log("joinCallBack!!!!!!!!!!!");
                self.joinRoom(joinCallBack.roomID);
            };
            joinCallBack.roomID = roomData[i].roomID;
            btnJoin.node.on(cc.Node.EventType.TOUCH_END, joinCallBack, self);

            var roomID = newRoomItem.getChildByName("roomID").getComponent("cc.Label");
            roomID.string = roomData[i].roomID;
            var bankerID = newRoomItem.getChildByName("bankerID").getComponent("cc.Label");
            bankerID.string = roomData[i].bankerID;
            var nickName = newRoomItem.getChildByName("nickName").getComponent("cc.Label");
            nickName.string = roomData[i].bankerName;
        }
        if(newRoomItemY < 510)
            newRoomItemY = 510;
        this.roomListContent.height = newRoomItemY + 100;
    },
    
    joinRoom:function(roomID){
        var self = this;
        console.log("joinRoom" + roomID);
        pomelo.request("connector.entryHandler.sendData", {"code" : "join","params" : {"roomId":parseInt(roomID)}}, function(data) {
            console.log("join join join!!!!!!!!!!");
            console.log(data);
            if(data.msg.code && data.msg.code == 13)
            {
                console.log("joinRoom data.code == 13 !!!!!!" + roomID);
                console.log(window.localSetting.roomHistory);
                for(var i in window.localSetting.roomHistory)
                {
                    if(parseInt(window.localSetting.roomHistory[i].roomID) == parseInt(roomID)){
                        console.log("asdasdasdas111111");
                        delete window.localSetting.roomHistory[i];
                        self.updateLayer();
                        window.localSetting.saveRoomHistory();
                    }
                }
                console.log(window.localSetting.roomHistory);
            }
            if(data.msg.code && data.msg.code == 119)
            {
                pomelo.disconnect();
                return;
            }
            if(!data.flag){
                if(data.msg && data.msg.code)
                    window.clientGlobal.tipsBox.showLayer(1,tipsConf[data.msg.code]);
                else
                    window.clientGlobal.tipsBox.showLayer(1,"加入房间失败,请重新创建!");
            }
        });
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit(); 
        this.updateLayer();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    }
});
